/*
  Travis Tarr
  tarrt@onid.oregonstate.edu
  CS362-400
  Homework #4

  Testing cardEffect:  adventurer

*/


#include <stdlib.h>
#include <stdio.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include <assert.h>
#include "rngs.h"
#include <time.h>

#define MAX_CARD 26
#define STARTING_HAND 5
#define NULL_CHOICE1 -1
#define NULL_CHOICE2 -1
#define NULL_CHOICE3 -1
#define DEF_BONUS 10
#define HANDPOS 0
#define HAND_AFTER_ADV 6  // 5 original hand + 2 treasures card - 1 discard

void setupHand(struct gameState *state, int player){
  int i, card;
  
  state->discardCount[player] = 0;
  // one of the cards needs to be 'adventurer'
  state->hand[player][HANDPOS] = adventurer;

  // rest are random
  for(i = 1; i < STARTING_HAND; i++){
    card = rand() % MAX_CARD;
    state->hand[player][i] = card;
  }
  state->handCount[player] = STARTING_HAND;
}

void setupDeck(struct gameState *state, int player){
  int i, card;

  // rest are random
  for(i = 0; i < MAX_DECK; i++){
    card = rand() % MAX_CARD;
    state->deck[player][i] = card;
  }

  state->deckCount[player] = MAX_DECK;
}


// check to see if card is in hand quantity number of times
int inHand(struct gameState *state, int player, int card){
  int i;
  for(i = 0; i < STARTING_HAND; i++){
    if(state->hand[player][i] == card){
      return 1;
    }
  }
  return 0;
}

// check to see if card is in discard quantity number of times
int inDiscard(struct gameState *state, int player, int card){
  int i;
  for(i = 0; i < state->discardCount[player]; i++){
    if(state->discard[player][i] == card){
      return 1;
    }
  }
  return 0;
}

int getTreasureCount(struct gameState *state, int player){
  int i, count = 0, card;

  for(i = 0; i < state->handCount[player]; i++){
    card = state->hand[player][i];
    if(card == copper || card == silver || card == gold)
      count++;
  }
  return count;
}


int main(int argc, char*argv[]) {
  time_t tm;
  srand((unsigned) time(&tm));
  struct gameState state;

  int i, j, player = 0, numPlayers = (rand() % 2) + 2, result, treasures, coin_bonus = DEF_BONUS;
  int r, seed = 1000;
  int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};

  for(i = 0; i < rand() % 100; i++){
    memset(&state, 23, sizeof(struct gameState));
    r = initializeGame(numPlayers, k, seed, &state);
    setupHand(&state, player);
    treasures = getTreasureCount(&state, player);
    
    printf("TEST Iteration: %d, Player: %d\n", i, player);
    
    result = cardEffect(adventurer, NULL_CHOICE1, NULL_CHOICE2, NULL_CHOICE3, &state, HANDPOS, &coin_bonus);
    
    if(result == 0){
      if(state.handCount[player] == HAND_AFTER_ADV)
	printf("TEST PASSED: non-treasure cards removed from hand\n");
      else
	printf("TEST FAILED: non-treasure cards possibly still in hand\n");

      if((treasures + 2) == getTreasureCount(&state, player))
	printf("TEST PASSED: 2 treasures added to player's hand\n");
      else
	printf("TEST FAILED: < 2 treasures added to player's hand\n");
      
      if(inDiscard(&state, player, adventurer) == 1)
	printf("TEST PASSED: adventurer card removed from hand\n");
      else
	printf("TEST FAILED: adventurer card not removed from hand\n");
    }
  }

  return 0;
}
